package com.gmail.yesguxun.tumblr.presenter;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.gmail.yesguxun.tumblr.Constant;
import com.gmail.yesguxun.tumblr.TrAccount;
import com.gmail.yesguxun.tumblr.content.TrContentObjectPool;
import com.gmail.yesguxun.tumblr.ui.SettingActivity;
import com.gmail.yesguxun.tumblr.ui.ViewBigPhotoActivity;
import com.gmail.yesguxun.util.EventBusAsync;
import com.gmail.yesguxun.util.EventBusEventObj;
import com.gmail.yesguxun.util.EventBusHandler;
import com.gmail.yesguxun.util.LogFactory;
import com.gmail.yesguxun.util.LogX;

public class TrSettingPresenter implements  EventBusHandler{
	
	SettingActivity mSettingActivity = null;
	private Handler mHandler = null;
	
	private LogX mLog = LogFactory.getLogObject();
	
	public TrSettingPresenter(SettingActivity activity)
	{
		this.mSettingActivity = activity;
		
		mHandler = new Handler();
		EventBusAsync.getInstance().registerEventHandler(Constant.EVENT_AUTHORIZATION_COMPLETE, this);
	}
	
	public void bind()
	{
		TrAccount acObj = TrAccount.getTrAccount();
		
		mSettingActivity.getEditTextEMail().setText(acObj.getEMail());
		mSettingActivity.getEditTextPassword().setText(acObj.getPassword());
			
		ArrayAdapter<CharSequence> myAdapt = (ArrayAdapter<CharSequence>) mSettingActivity.getSelectPictueSizeSpinner().getAdapter();
		mSettingActivity.getSelectPictueSizeSpinner().setSelection(myAdapt.getPosition(acObj.getSizeSelection()));
		ArrayAdapter<CharSequence> myAdapt1 = (ArrayAdapter<CharSequence>) mSettingActivity.getSelectViewModeSpinner().getAdapter();
		mSettingActivity.getSelectViewModeSpinner().setSelection(myAdapt1.getPosition(acObj.getViewModeSelection()));
	
		
		this.mSettingActivity.getButtonAuthorize().setOnClickListener(new View.OnClickListener() {
			
            public void onClick(View v) {
            	
                String email = mSettingActivity.getEditTextEMail().getText().toString();
                String password = mSettingActivity.getEditTextPassword().getText().toString();
                String sizeSelect = mSettingActivity.getSelectPictueSizeSpinner().getSelectedItem().toString();
                String viewModeSelect = mSettingActivity.getSelectViewModeSpinner().getSelectedItem().toString();
                
                //check if the input is valid or not
                if(email == null || email.equals("") || password == null || password.equals(""))
                {
                	mSettingActivity.onInvalidInput();              	
                	return;
                }
                else
                {
                	TrAccount acObj = TrAccount.getTrAccount();
                	acObj.setEMail(email);
                	acObj.setPassword(password);
                	acObj.setSizeSelection(sizeSelect);
                	acObj.setViewModeSelection(viewModeSelect);
                	acObj.save(); 
                	        
                	//instead of go to view, we need to start the authorization here.
                	
                    if(acObj.getLoginState() == false)
                    {
                	    TrContentObjectPool.getPoolInstance().authorizeAsync();
                	    mSettingActivity.onStartAuthorization();
                    }
                    else
                    {
                    	onAuthorizeComplete(true);
                    }
                }              
            }
        });
	}
	
	private void onAuthorizeComplete(boolean resultAuthorize)
	{
		mSettingActivity.onEndAuhorization(resultAuthorize);
		
		if(resultAuthorize == true)
		{
			mLog.debug("Authorization passed, Starting ViewBigPhotoActivity");
			
			TrAccount.getTrAccount().setLoginState(true);
			Intent myIntent = new Intent(mSettingActivity.getContext(), ViewBigPhotoActivity.class);
			mSettingActivity.startActivity(myIntent);
		}
		else
		{
			mLog.debug("tumblr", "Authorization Failed");
			
			//clear the login information
			TrAccount.getTrAccount().setLoginState(false);
		}
	}
	

	@Override
	public void onEvent(EventBusEventObj eventObj) {
		if(eventObj.getEventType().equals(Constant.EVENT_AUTHORIZATION_COMPLETE))
		{
			Boolean result = (Boolean)eventObj.getEventData();
			mLog.debug("tumblr", "Processing event:" + Constant.EVENT_AUTHORIZATION_COMPLETE);
            if(result.booleanValue() == true)
            {
                this.mHandler.post(new Runnable(){                    
                    @Override
                    public void run() {
                    	onAuthorizeComplete(true);                        
                    }
                });                
            }
            else
            {
            	this.mHandler.post(new Runnable(){                    
                    @Override
                    public void run() {
                    	onAuthorizeComplete(false);                        
                    }
                });                
            }
		}
		
	}
}
